为了账号安全,请及时绑定邮箱和手机立即绑定

SSH Web工程环境搭建总结

标签:
MySQL


SSH(Struts+Spring+Hibernate)框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题。以前一直用的是Struts+Hibernate做项目,今天第一次搭建SSH环境,也遇到了很多问题,下面我介绍一下SSH框架搭建的全过程,以供后面学习的人少走点弯路,照我的做法一步一步的做就没有什么问题了。

第一步:准备工作:Eclspe的安装以及安装Hibernate、Spring插件在这里我就不多讲了,最简单的方式是在Eclspe的Help菜单中有个Eclspe Maketplace菜单项,点击进去就可以看到Spring、JBoss之类的Makets,单Install就会安装了,只要安提示来就好了。下载好Struts、Spring、Hibernate,目前最新的应该是Struts2.3.7、Spring3.1.2、Hibernate4.2.0,后面我也会将里面jar的包上传到资源库中,大家方便下。

第二步:项目配置

    1、新建Web项目,名字自己取 如:FirstSSHDemo

        2、在WebContent/WEB-INF目录下创建或修改(如果系统自动创建好的)web.xml文件,增加Struts2拦截器,如下代码:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 

    id="WebApp_ID" version="2.5"> 

 

    <!-- struts2 --> 

    <filter> 

        <filter-name>struts2</filter-name> 

        <filter-class> 

            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 

    </filter> 

    <filter-mapping> 

        <filter-name>struts2</filter-name> 

        <url-pattern>/*</url-pattern> 

    </filter-mapping> 

 

 

</web-app> 

    3、在Java Resources/src目录下建立Struts的配置文件struts.xml 内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>  

<!DOCTYPE struts PUBLIC  

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  

"http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts> 

    <package namespace="/" name="ecshop_pro" extends="struts-default"> 

<!-- action根据自己的需求来配置 --> 

        <action name="index" > 

            <result name="input">index.jsp</result> 

        </action> 

    </package> 

<!-- 以下是我自己工程的东西搭环境时将include这些全部删除,预留下的意思是希望大家以后这样来配置,每个功能模块一个xml配置,然后将其include进来就好了  --> 

    <include file="front.xml" /> 

    <include file="login.xml" /> 

    <include file="user.xml" /> 

    <include file="goods.xml" /> 

    <include file="order.xml" /> 

    <include file="article.xml" /> 

    <include file="shopping.xml" /> 

</struts>  

   4、配置Spring环境,在这里我们先讲配置文件的写法,后面统一的引入三个框架所需要的Jar包。我们在前面的web.xml中增加Spring的监听器,代码如下

 

<!-- Spring Framework 说明一下:我这里是将applicationContext.xml放在/WEB-INF目录中,所以<param-value>是这种写法 如果把它放在Java Resources/src 下 将/WEB-INF改成classpath:即可 --> 

    <listener> 

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 

    </listener> 

    <context-param> 

        <param-name>contextConfigLocation</param-name> 

        <param-value>/WEB-INF/applicationContext.xml</param-value> 

    </context-param> 

   5、在第四步中我们已经看到了applicationContext.xml,这个就是Spring的配置文件了,

在WebContent/WEB-INF目录下创applicationContext.xml文件,在关于具体的详细配置说明可以去官网查看,

这里只是带大家搭建下开发环境,代码如下 

 

<?xml version="1.0" encoding="UTF-8"?> 

<beans  xmlns="http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 

    xmlns:tx="http://www.springframework.org/schema/tx" 

    xsi:schemaLocation="  

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 

    <!-- Action 我这里配置了一个后台登陆的Action Bean 这里需要注意一下scope这个属性,

它有两个值:prototype和singleton,在使用prototype值是没有什么需注意的,

使用singleton时如果配置property,那么需要注意的是property 的name的值必须是Action中需要依赖注入的那个

变量,也就是指向Bo或Dao或Service层相应实现类的变量;ref指定了属性对BeanFactory中其他Bean的引用关系,

必须为其他Bean的id --> 

     <bean id="loginAction" scope="prototype" class="com.ecshop.action.LoginAction">  

          <property name="IAdminDao" ref="ado"></property>  

    </bean> 

     

    <!-- Dao --> 

        <!-- id为Action中的变量 --> 

    <bean id="ado" class="com.ecshop.dao.impl.HAdminDao"></bean> 

     

    <!-- sessionFactory --> 

    <bean id="sessionFactory" 

        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

        <property name="configLocation"> 

            <value>classpath:/hibernate.cfg.xml</value> 

<!-- 这里注意一下上面的value标签内的值,这是配置Hibernate配置文件的地址和文件名, 我的hibernate.cfg.xml

是放在Java Resouses/src根目录中所以这样写,如果只写hibernate.cfg.xml 会报BeanCreationException和FileNotFondException --> 

        </property> 

    </bean> 

 

</beans>  

    6、在Java Resouses/src根目录建立Hibernate配置文件hibernate.cfg.xml,代码结构如下:

 

<?xml version="1.0" encoding="utf-8"?> 

<!DOCTYPE hibernate-configuration PUBLIC 

"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 

<!--  我使用的是MySQL数据库所以我的drive_class就是MySQL的驱动类别,相应的url、password、username、dialect大家根据自己的项目需求更改就好了 -->

 

        <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

        <property name="hibernate.connection.password">lossie</property> 

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ecshop</property> 

        <property name="hibernate.connection.username">root</property> 

        <property name="hibernate.current_session_context_class">thread</property> 

<!-- SQL dialect 方言,其他数据库的可查Hibernate文档中的Hibernate数据库方言 -->

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

<!-- 格式化输出SQL -->       

 <property name="hibernate.format_sql">true</property> 

<!-- show_sql 生成SQL语句 -->

         <property name="show_sql">false</property> 

        <property name="hibernate.search.autoregister_listeners">false</property> 

 <!--  下面是包含javaBean与数据库表的映射文件 -->

        <mapping resource="com/ecshop/vo/EcsAdminUser.hbm.xml" /> 

        

    </session-factory> 

</hibernate-configuration> 

第三步:将SSH要使用的jar包引入工程中的WEB-INF/lib目录中,然后将项目加入到Tomcat中启动运行看控制台是否有错误产生,如果没有错误即环境搭建完成

Struts依赖的包(包括了Spring需要使用的公用包):  

 

Spring依赖的包(必须的这几个):

 

Hibernate所依赖的包:

 

©著作权归作者所有:来自51CTO博客作者liusheng344的原创作品,如需转载,请注明出处,否则将追究法律责任

SSH项目环境搭建【J2EE】


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消